import { Body, Controller, Get, ParseArrayPipe, Post, Query } from "@nestjs/common";
import { CreateLog, Roles } from "@/common/decorators";
import { Role } from "@/common/enum/role.enum";
import { RequiredPipe } from "@/common/pipes/required.pipe";
import { SysDictItem } from "@/database/entity/SysDictItem";
import { DictItemService } from "./dict-item.service";
import { CreateDictItemDto } from "./dto/create-dict-item.dto";
import { UpdateDictItemDto } from "./dto/update-dict-item.dto";

@Controller("sys/dictItem")
export class DictItemController {
    constructor(private readonly dictItemService: DictItemService) {}

    /** 新增字典项 */
    @CreateLog({ module: "系统字典", type: "create", action: "创建字典项" }, SysDictItem)
    @Roles(Role.SUPER_ADMIN)
    @Post("create")
    async create(@Body() data: CreateDictItemDto) {
        return this.dictItemService.create(data);
    }

    /** 删除字典项 */
    @CreateLog({ module: "系统字典", type: "delete", action: "删除字典项" }, SysDictItem)
    @Roles(Role.SUPER_ADMIN)
    @Post("remove")
    async remove(@Body("id", RequiredPipe) id: string) {
        return this.dictItemService.remove(id);
    }

    /** 更新字典项 */
    @CreateLog({ module: "系统字典", type: "update", action: "更新字典项" }, SysDictItem)
    @Roles(Role.SUPER_ADMIN)
    @Post("update")
    async update(@Body() data: UpdateDictItemDto) {
        return this.dictItemService.update(data);
    }

    /** 设为默认值 */
    @CreateLog({ module: "系统字典", type: "update", action: "更新字典项默认值" }, SysDictItem)
    @Roles(Role.SUPER_ADMIN)
    @Post("updateDefault")
    async updateDefault(@Body("id", RequiredPipe) id: string, @Body("typeCode", RequiredPipe) typeCode: string) {
        return this.dictItemService.updateDefault(id, typeCode);
    }

    /** 取消默认 */
    @CreateLog({ module: "系统字典", type: "update", action: "取消字典项默认值" }, SysDictItem)
    @Roles(Role.SUPER_ADMIN)
    @Post("cancelDefault")
    async cancelDefault(@Body("id", RequiredPipe) id: string) {
        return this.dictItemService.cancelDefault(id);
    }

    /** 查询单个记录 */
    @Get("findById")
    async findById(@Query("id", RequiredPipe) id: string) {
        return this.dictItemService.findById(id);
    }

    /** 获取下拉选项 */
    @Get("listOptions")
    async listOptions(@Query("typeCode", RequiredPipe, new ParseArrayPipe({ items: String })) typeCodes: string[]) {
        return this.dictItemService.listOptions(typeCodes);
    }

    /** 获取列表 */
    @Get("list")
    async list(@Query("typeCode", RequiredPipe) typeCode: string) {
        return this.dictItemService.list(typeCode);
    }
}
